﻿using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using System.Collections.Generic;

partial class Level : GameObjectList
{
    protected bool locked, solved;
    protected Button quitButton;
    protected TimerGameObject timer;
    protected SpriteGameObject timerBackground;
    protected VisibilityTimer hintTimer;
    protected GameObjectList hintfield;
    protected List<GameObjectList> backgrounds = new List<GameObjectList>();
    protected SpriteGameObject staticBackground;
    protected TileField tiles;
    protected int levelHeight, levelWidth;

    public Level(int levelIndex)
    {
        // load the backgrounds
        staticBackground = new SpriteGameObject("Backgrounds/spr_sky");
        staticBackground.Position = new Vector2(0, GameEnvironment.Screen.Y - staticBackground.Height);
        
        // add a few random mountains
        loadMountainLayer(10);
        loadMountainLayer(5);

        //Add clouds in back layer
        GameObjectList cloudList = new GameObjectList(0, "clouds");
        Clouds clouds = new Clouds(2);
        cloudList.Add(clouds);
        backgrounds.Add(cloudList);

        timerBackground = new SpriteGameObject("Sprites/spr_timer", 100);
        timerBackground.Position = new Vector2(10, 10);

        quitButton = new Button("Sprites/spr_button_quit", 100);
        quitButton.Position = new Vector2(GameEnvironment.Screen.X - quitButton.Width - 10, 10);

        this.Add(new GameObjectList(1, "waterdrops"));
        this.Add(new GameObjectList(2, "enemies"));

        this.LoadTiles("Content/Levels/" + levelIndex + ".txt");
    }
    void loadMountainLayer(int count)
    {
        GameObjectList mountains = new GameObjectList(0, "mountains");
        for (int i = 0; i < count; i++)
        {
            SpriteGameObject mountain = new SpriteGameObject("Backgrounds/spr_mountain_" + (GameEnvironment.Random.Next(2) + 1), 1);
            mountain.Position = new Vector2((float)GameEnvironment.Random.NextDouble() * GameEnvironment.Screen.X - mountain.Width / 2, GameEnvironment.Screen.Y - mountain.Height * 1.4f);
            mountains.Add(mountain);
        }
        backgrounds.Add(mountains);
    }
    void CreateTimer(int time)
    {
        timer = new TimerGameObject(time, 101, "timer");
        timer.Position = new Vector2(25, 30);
    }



    public bool Completed
    {
        get
        {
            SpriteGameObject exitObj = this.Find("exit") as SpriteGameObject;
            Player player = this.Find("player") as Player;
            if (!exitObj.CollidesWith(player))
                return false;
            GameObjectList waterdrops = this.Find("waterdrops") as GameObjectList;
            foreach (GameObject d in waterdrops.Objects)
                if (d.Visible)
                    return false;
            return true;
        }
    }
    public int Height
    {
        get { return levelHeight; }
    }
    public int Width
    {
        get { return levelWidth; }
    }
    public TimerGameObject Timer
    {
        get { return timer; }
    }

    public TileField Tiles
    {
        get { return tiles; }
    }
    public bool GameOver
    {
        get
        {
            Player player = this.Find("player") as Player;
            return !player.IsAlive || timer.GameOver;
        }
    }

    public bool Locked
    {
        get { return locked; }
        set { locked = value; }
    }

    public bool Solved
    {
        get { return solved; }
        set { solved = value; }
    }

    public override void Draw(GameTime gameTime, Microsoft.Xna.Framework.Graphics.SpriteBatch spriteBatch)
    {

        staticBackground.Draw(gameTime, spriteBatch);
        spriteBatch.End();

        //Draw every backgound list in a layer moving backwards
        for (int i = backgrounds.Count - 1; i >= 0; i--)
        {
            Camera2D cam = new Camera2D();
            cam.CameraPosition = GameEnvironment.GameCamera.CameraPosition / (i + 2);

            //Draw the layer
            spriteBatch.Begin(SpriteSortMode.Deferred, null, null, null, null, null, cam.TransformMatrix);
            backgrounds[i].Draw(gameTime, spriteBatch);
            spriteBatch.End();
        }
        spriteBatch.Begin(SpriteSortMode.Deferred, null, null, null, null, null, GameEnvironment.GameCamera.TransformMatrix);
        base.Draw(gameTime, spriteBatch);
        spriteBatch.End();

        spriteBatch.Begin(SpriteSortMode.Deferred, null, null, null, null, null, GameEnvironment.SpriteScale);
        quitButton.Draw(gameTime, spriteBatch);
        timerBackground.Draw(gameTime, spriteBatch);
        timer.Draw(gameTime, spriteBatch);
        hintfield.Draw(gameTime, spriteBatch);
    }
}

